package com.xiaoshuidi.cloud.module.rooms.repository;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaoshuidi.cloud.framework.common.pojo.CommonResult;
import com.xiaoshuidi.cloud.module.rooms.mapper.RoomFloorMapper;
import com.xiaoshuidi.cloud.module.rooms.pojo.RoomFloor;
import lombok.NonNull;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Component;

import java.util.List;
import java.util.Map;

@Component
@RequiredArgsConstructor
public class RoomFloorRepository extends ServiceImpl<RoomFloorMapper, RoomFloor> {
    private final RoomFloorMapper roomFloorMapper;

    /**
     * 删除公寓下的所有楼层
     * @param apartmentId
     * @return
     */
    public CommonResult<Boolean> delByApartmentId(Long apartmentId) {
        LambdaQueryWrapper<RoomFloor> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomFloor::getApartmentId,apartmentId);
        roomFloorMapper.delete(wrapper);
        return CommonResult.success(Boolean.TRUE);
    }

    public RoomFloor myGetById(Long floorId) {
        RoomFloor roomFloor = this.getById(floorId);
        Assert.isTrue(ObjectUtil.isNotEmpty(roomFloor), "楼层信息不存在id:{}", floorId);
        return roomFloor;
    }

    public List<RoomFloor> getByApartmentAreaId(@NonNull Long apartmentAreaId) {
        LambdaQueryWrapper<RoomFloor> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomFloor::getApartmentAreaId, apartmentAreaId);
        return this.list(wrapper);
    }

    public List<RoomFloor> getByApartmentAreaIdAndNum(Long apartmentAreaId, List<Integer> inFloorNum) {
        LambdaQueryWrapper<RoomFloor> wrapper = Wrappers.lambdaQuery();
        wrapper.eq(RoomFloor::getApartmentAreaId, apartmentAreaId);
        wrapper.in(RoomFloor::getNum, inFloorNum);
        return this.list(wrapper);
    }
}
